﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SeparateProjects
{
    public partial class Pro_User_Registration : Form
    {
        public bool cut = true;
        public Pro_User_Registration()
        {
            InitializeComponent();
        }
        #region 判断是否有重复数据
        private void Pro_User_Registration_Load(object sender, EventArgs e)
        {
            var code = 0;
            //文本框禁用
            this.txt_Sid.Enabled = false;
            Random_Number();
            string str = @"Data Source = DESKTOP-28VSNS5;
                           Initial Catalog = Training_Project;
                           Uid = Lv;
                           Pwd = 123456";
            var coon = new SqlConnection(str);
            try
            {
                coon.Open();
                var sql = string.Format(@"select count(*) from Stu_Login
                                        where Sid = {0};", code);
                var comm = new SqlCommand(sql, coon);
                object obj = comm.ExecuteScalar();
                int number = Convert.ToInt32(obj);
                if (number > 0)
                {
                    Random_Number();
                }
            }
            catch (Exception error)
            {
                Console.WriteLine("数据库异常，请联系管理员：      " + error.Message);
            }
            finally
            {
                coon.Close();
            }
        }
        #endregion
        #region 随机生成学号
        private void Random_Number()
        {
            //随机生成数据
            Random rand = new Random();
            var code = rand.Next(100000, 1000000);
            this.txt_Sid.Text = code.ToString();
        }
        #endregion
        #region   注册界面跳转登录界面
        /// <summary>
        /// 跳转登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbl_DengLu_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Pro_User_Login con = new Pro_User_Login();
            con.Show();
            this.Hide();
        }
        #endregion
        #region  按钮变色
        /// <summary>
        /// 按钮变色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_User_Click(object sender, EventArgs e)
        {
            if ((btn_User.Focused) == cut)
            {
                //点击切换后的颜色
                this.btn_User.BackColor = Color.FromArgb(55, 55, 55);
                //原来颜色
                this.btn_Admin.BackColor = Color.FromArgb(98, 98, 98);
            }
            cut = true;
            txt_Sid.Clear();
            txt_Password.Clear();
            Random_Number();
        }
        private void btn_Admin_Click(object sender, EventArgs e)
        {
            if ((btn_Admin.Focused) == cut)
            {
                //点击切换后的颜色
                btn_Admin.BackColor = Color.FromArgb(55, 55, 55);
                //原来颜色
                this.btn_User.BackColor = Color.FromArgb(98, 98, 98);
            }
            cut = false;
            txt_Sid.Clear();
            txt_Password.Clear();
            Random_Number();
        }
        #endregion
        #region   注册验证
        /// <summary>
        /// 注册验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ZhuCe_Click(object sender, EventArgs e)
        {
            var UserName = txt_Sid.Text.Trim();
            var Password = txt_Password.Text.Trim();
            if (string.IsNullOrWhiteSpace(Password))
            {
                MessageBox.Show("密码不能为空", "提示信息",
                                 MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_Password.Focus();
            }
            if (cut.Equals(false))
            {
                //MessageBox.Show("管理员");
                string str = @"Data Source = DESKTOP-28VSNS5;
                                Initial Catalog = Training_Project;
                                Uid = Lv;Pwd = 123456;";
                var coon = new SqlConnection(str);
                try
                {
                    coon.Open();
                    var sql = string.Format(@"insert into Manager_Login(UserName,Password)
                                            values('{0}','{1}')", UserName, Password);
                    var comm = new SqlCommand(sql, coon);
                    int number = comm.ExecuteNonQuery();
                    if (number > 0)
                    {
                        DialogResult dr = MessageBox.Show("添加成功", "提示信息",
                                             MessageBoxButtons.OKCancel,
                                             MessageBoxIcon.Information);
                        if (dr.Equals(DialogResult.OK))
                        {
                            Pro_User_Login abc = new Pro_User_Login();
                            abc.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        MessageBox.Show("添加失败", "提示信息",
                                            MessageBoxButtons.YesNo,
                                            MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("服务器异常，请联系管理员" + ex.Message);
                }
                finally
                {
                    coon.Close();
                }
            }
            else
            {
                //MessageBox.Show("用户");
                string str = @"Data Source = DESKTOP-28VSNS5;
                                Initial Catalog = Training_Project;
                                Uid = Lv;Pwd = 123456;";
                var coon = new SqlConnection(str);
                try
                {
                    coon.Open();
                    var sql = string.Format(@"insert into Stu_Login(UserName,Password)
                                            values('{0}','{1}')", UserName, Password);
                    var comm = new SqlCommand(sql, coon);
                    int number = comm.ExecuteNonQuery();
                    if (number > 0)
                    {
                        DialogResult dr = MessageBox.Show("添加成功", "提示信息",
                                             MessageBoxButtons.OKCancel,
                                             MessageBoxIcon.Information);
                        if (dr.Equals(DialogResult.OK))
                        {
                            Pro_User_Login abc = new Pro_User_Login();
                            abc.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        MessageBox.Show("添加失败", "提示信息",
                                            MessageBoxButtons.YesNo,
                                            MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("服务器异常，请联系管理员" + ex.Message);
                }
                finally
                {
                    coon.Close();
                }
            }
        }
        #endregion
        #region  键盘按下点击事件
        /// <summary>
        /// 键盘按下点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Sid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue.Equals(13))
            {
                this.btn_ZhuCe_Click(null, null);
            }
        }
        #endregion
    }
}
